package aria.my12306;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TicketDetailStepActivity extends AppCompatActivity {
    private ListView lvTicketDetailStep;
    List<Map<String,Object>> data = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket_detail_step);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.back_25);
        actionBar.setDisplayShowHomeEnabled(false);

        lvTicketDetailStep = (ListView)findViewById(R.id.lvTicketDetailStep);
        data = new ArrayList<Map<String, Object>>();

        Map<String,Object> row1 = new HashMap<String,Object>();
        row1.put("seatName","高级软座");
        row1.put("seatNum","200张");
        row1.put("seatPrice","￥188.5");
        data.add(row1);

        Map<String,Object> row2 = new HashMap<String,Object>();
        row2.put("seatName","硬座");
        row2.put("seatNum","88张");
        row2.put("seatPrice","￥148.5");
        data.add(row2);

        Map<String,Object> row3 = new HashMap<String,Object>();
        row3.put("seatName","无座");
        row3.put("seatNum","100张");
        row3.put("seatPrice","￥148.5");
        data.add(row3);

        lvTicketDetailStep.setAdapter(new TicketDetailStepAdapter());
    }

    class TicketDetailStepAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //如果对象不存在，创建；存在，取出（行）
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                //创建conventView（加载行布局）
                convertView = LayoutInflater.from(
                        TicketDetailStepActivity.this
                ).inflate(R.layout.item_ticket_detail_step,null);


                viewHolder.tvTicketDetailStepSeatName = (TextView)convertView
                        .findViewById(R.id.tvTicketDetailStepSeatName);
                viewHolder.tvTicketDetailStepSeatNum = (TextView)convertView
                        .findViewById(R.id.tvTicketDetailStepSeatNum);
                viewHolder.tvTicketDetailStepSeatPrice = (TextView)convertView
                        .findViewById(R.id.tvTicketDetailStepSeatPrice);
                viewHolder.btnTicketDetailStepSeatOrder = (Button)convertView
                        .findViewById(R.id.btnTicketDetailStepSeatOrder);

                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder)convertView.getTag();
            }
            viewHolder.tvTicketDetailStepSeatName.setText(data.get(position)
                    .get("seatName").toString());
            viewHolder.tvTicketDetailStepSeatNum.setText(data.get(position)
                    .get("seatNum").toString());
            viewHolder.tvTicketDetailStepSeatPrice.setText(data.get(position)
                    .get("seatPrice").toString());
            viewHolder.btnTicketDetailStepSeatOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(TicketDetailStepActivity.this,"button click",
                            Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;
        }
    }
    class ViewHolder {
        TextView tvTicketDetailStepSeatName;
        TextView tvTicketDetailStepSeatNum;
        TextView tvTicketDetailStepSeatPrice;
        Button btnTicketDetailStepSeatOrder;

    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
